package com.sitech.portal.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.sitech.ibnms.common.form.TreeNode;
import com.sitech.ibnms.common.service.IAsyncTreeService;
import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.portal.system.dao.ISysDeptDao;
import com.sitech.portal.system.dao.ISysRoleDao;
import com.sitech.portal.system.domain.TblSYS_COMPANY;
import com.sitech.portal.system.domain.TblSYS_DEPT;
import com.sitech.portal.system.domain.TblSYS_MODULE;
import com.sitech.portal.system.service.ISysDeptService;

public class SysDeptServiceImpl implements ISysDeptService,IAsyncTreeService {

	private ISysDeptDao sysDeptDao;
    private ISysRoleDao sysRoleDao;

    public void setSysRoleDao(ISysRoleDao sysRoleDao) {
        this.sysRoleDao = sysRoleDao;
    }
	public ISysDeptDao getSysDeptDao() {
		return sysDeptDao;
	}

	public void setSysDeptDao(ISysDeptDao sysDeptDao) {
		this.sysDeptDao = sysDeptDao;
	}

	@Override
	public void add(TblSYS_DEPT tblSYS_DEPT) {
		String parentId = tblSYS_DEPT.getSUPER_DEPT_ID();
		String id = sysDeptDao.findMaxDeptIdBySuperDeptId(parentId);
		if(id==null){
			if(parentId.equals("0"))
				id = "10";
			else
				id = parentId + "01";
		}else
			id = "" + (Integer.parseInt(id) + 1);
		tblSYS_DEPT.setDEPT_ID(id);
		if(id.length()>2)
			tblSYS_DEPT.setCOMP_ID(id.substring(0,2));
		sysDeptDao.add(tblSYS_DEPT);
	}

	@Override
	public void delForTB_SYS_MODULE(String pre_module_id, String module_name) {
		// TODO Auto-generated method stub
		sysDeptDao.delForTB_SYS_MODULE(pre_module_id, module_name);
	}

	@Override
	public void delete(TblSYS_DEPT tblSYS_DEPT) {
		// TODO Auto-generated method stub
		sysDeptDao.delete(tblSYS_DEPT);
	}

	@Override
	public List findByDeptId(String DeptId) {
		// TODO Auto-generated method stub
		return sysDeptDao.findByDeptId(DeptId);
	}

	@Override
	public List findBySuperDeptId(String strSuperDeptId) {
		// TODO Auto-generated method stub
		return sysDeptDao.findBySuperDeptId(strSuperDeptId);
	}

	@Override
	public int findCountBySuperDeptId(String deptId) {
		// TODO Auto-generated method stub
		return sysDeptDao.findCountBySuperDeptId(deptId);
	}

	@Override
	public HashMap findDepartAdminByDeptId(String DEPT_ID) {
		// TODO Auto-generated method stub
		return sysDeptDao.findDepartAdminByDeptId(DEPT_ID);
	}

	@Override
	public HashMap findDepartAreaByDeptId(String DEPT_ID) {
		// TODO Auto-generated method stub
		return sysDeptDao.findDepartAreaByDeptId(DEPT_ID);
	}

	@Override
	public TblSYS_DEPT findDepartByPK(String s) {
		// TODO Auto-generated method stub
		return sysDeptDao.findDepartByPK(s);
	}

	@Override
	public List findDepartForBulletin() {
		// TODO Auto-generated method stub
		return sysDeptDao.findDepartForBulletin();
	}

	@Override
	public List findDepartHavaChildBySuperDeptId(String SUPER_DEPT_ID) {
		// TODO Auto-generated method stub
		return sysDeptDao.findDepartHavaChildBySuperDeptId(SUPER_DEPT_ID);
	}

	@Override
	public List findForOptions() {
		// TODO Auto-generated method stub
		return sysDeptDao.findForOptions();
	}

	@Override
	public List findMgrIdEmployee(String DeptId) {
		// TODO Auto-generated method stub
		return sysDeptDao.findMgrIdEmployee(DeptId);
	}

	@Override
	public List findRootDepartByCompId(String strCompId) {
		// TODO Auto-generated method stub
		return sysDeptDao.findRootDepartByCompId(strCompId);
	}

	@Override
	public List findRootDepartByCompIdAndManageUnitID(String strCompId,
			String manage_unit_id) {
		// TODO Auto-generated method stub
		return sysDeptDao.findRootDepartByCompIdAndManageUnitID(strCompId, manage_unit_id);
	}

	@Override
	public List findRootDepartHavaChildByCompId(String COMP_ID) {
		// TODO Auto-generated method stub
		return sysDeptDao.findRootDepartByCompId(COMP_ID);
	}

	@Override
	public void update(TblSYS_DEPT tblSYS_DEPT) {
		// TODO Auto-generated method stub
		sysDeptDao.update(tblSYS_DEPT);
	}

	@Override
	public TreeNode getRootNode(String loginId,
			String rootId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public IPage findListByParentId(TblSYS_DEPT tblSYS_DEPT, PageForm pageForm) {
		// TODO Auto-generated method stub
		return sysDeptDao.findListByParentId(tblSYS_DEPT, pageForm);
	}
	
	//查找用户负责的部门
	public List<HashMap> findDeptByMgrId(String mgrId){
		return sysDeptDao.findDeptByMgrId(mgrId);
	}
	
	public List findDeptNodeInfo(String deptId){
		return sysDeptDao.findDeptNodeInfo(deptId);
	}
	

	@Override
	public List<TreeNode> getSubNodes(
			String loginId, TreeNode pnode)
			throws Exception {
		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		
		String strSuperDeptId = pnode.getId()==null?"0":pnode.getId();
		
		List<HashMap> result = null;
        Boolean isAdmin =   sysRoleDao.isAdmin(loginId);
		if(strSuperDeptId.equals("0")&&!isAdmin){
			List<HashMap> list = sysDeptDao.findDeptByMgrId(loginId);
			if(list!=null&&list.size()>0){
				HashMap map = list.get(0);
				String deptId = map.get("DEPT_ID").toString();
				result = this.findBySuperDeptId(deptId);
			}else{
				result = this.findBySuperDeptId(strSuperDeptId);
			}
		}else{
			result = this.findBySuperDeptId(strSuperDeptId);
		}
		for(HashMap dept : result){
			TreeNode treeNode = new TreeNode();
			treeNode.setId(dept.get("DEPT_ID").toString());
			treeNode.setName(dept.get("DEPT_NAME").toString());
			treeNode.setpId(pnode.getId());
			if(dept.get("CHILDREN").toString().equals("0"))
				treeNode.setIsParent(false);
			treeNodes.add(treeNode);
		}
		return treeNodes;
	}
	
	

}
